const request = require('request');
const querystring = require('querystring');
const config = require('../config/config.js')

async function doGet(params) {
	let {
		url,
		...param
	} = params
	param.apikey = config.apikey
	let queryStr = querystring.stringify(param);
	let requestUrl = config.apiRootUrl + '/' + url + '?' + queryStr
	console.log('doget:' + requestUrl);
	console.log('param:' + JSON.stringify(param));
	const res = await uniCloud.httpclient.request(requestUrl, {
		method: 'GET',
		data: {

		},
		dataType: 'json',
		header: {
			'content-type': 'application/json'
		}
	})
	// console.log('res');
	// console.log(JSON.stringify(res));
	if (res.data) {
		return res.data
	}
	return null
}

async function doPost(params) {
	await request({
			url: config.apiRootUrl + '/' + url,
			method: "POST",
			json: true,
			headers: {
				"content-type": "application/json",
			},
			body: param
		},
		(err, rep, body) => {
			if (err) {
				console.log(" request 请求get请求出现错误 err:", err)
				return err;
			}
			// body表示返回的数据
			if (body) {
				// 请求成功
				return body
			}
		})
}
module.exports = {
	doGet,
	doPost
}